// pages/detail/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    content:'',
    contentArr:[],
    item:{},
    origin_item:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      item:JSON.parse(decodeURIComponent(options.item)),
      origin_item:options.item
    })
    wx.setNavigationBarTitle({
      title:this.data.item.title
    })
    let color = ['#e54d42','#f37b1d','#fbbd08','#8dc63f','#39b54a','#1cbbb4','#0081ff','#6739b6','#9c26b0','#e03997','#a5673f']
    let num = parseInt(Math.random()*color.length)-1
    wx.setNavigationBarColor({
      backgroundColor: color[num],
      frontColor: '#ffffff',
    })
    this.next_text()
  },

  // 上一条
  pre_text(){
    wx.vibrateShort({
      type:'light'
    })
    let arr = this.data.contentArr
    if(arr.length == 1){
      return
    }
    arr.pop()
    console.log(arr[arr.length-1])
    this.setData({
      contentArr:arr,
      content:arr[arr.length-1]
    })
  },

  // 复制
  copy_text(){
    wx.vibrateShort({
      type:'light'
    })
    wx.setClipboardData({
      data: this.data.content,
    })
  },

  // 下一条
  next_text(){
    wx.vibrateShort({
      type:'light'
    })
    wx.showNavigationBarLoading()
    let that = this
    let item = this.data.item
    wx.request({
      url: item.api,
      method:'GET',
      success(res){
        if(item.type == 1){
          that.handle_one(res)
        }else if(item.type == 2){
          that.handle_two(res)
        }else if(item.type == 3){
          that.handle_three(res)
        }else if(item.type == 4){
          that.handle_four(res)
        }
      }
    })
  },

  // 处理方式 https://api.shadiao.pro 类型1
  handle_one(obj){
    wx.hideNavigationBarLoading()
    this.setData({
      content:obj.data.data.text
    })
    let arr = this.data.contentArr
    arr.push(obj.data.data.text)
    this.setData({
      contentArr:arr
    })
  },

  handle_two(obj){
    wx.hideNavigationBarLoading()
    this.setData({
      content:obj.data.hitokoto
    })
    let arr = this.data.contentArr
    arr.push(obj.data.hitokoto)
    this.setData({
      contentArr:arr
    })
  },

  handle_three(obj){
    wx.hideNavigationBarLoading()
    this.setData({
      content:obj.data
    })
    let arr = this.data.contentArr
    arr.push(obj.data)
    this.setData({
      contentArr:arr
    })
  },

  handle_four(obj){
    console.log(obj)
    wx.hideNavigationBarLoading()
    this.setData({
      content:obj.data.data.content
    })
    let arr = this.data.contentArr
    arr.push(obj.data.data.content)
    this.setData({
      contentArr:arr
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title:this.data.content,
      path:'pages/detail/detail?item='+this.data.origin_item
    }
  }
})